<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <!-- <style>
    table{
      width: 600px;
      border: 1px solid red;
      border-spacing: 0;
      border-collapse: collapse;
    }
    th,td{
      border: 1px solid red;
      text-align: center;
    }
  </style> -->
</head>
<body>
  <input type="text" id="text">
  <input type="button" id="btn" value="搜索">
  <table border="4" width="600">
    <thead>
      <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
      </tr>
    </thead>
    <tbody>
      <!-- <tr>
        <td>1</td>
        <td>罗小黑</td>
        <td>2</td>
        <td>男</td>
      </tr> -->
    </tbody>
  </table>
  <script src="jquery.min.js"></script>
  <script>
    $('#btn').click(function(){
      var keyValue = $('#text').val();
      $.get('/api/searchStudent','name='+keyValue,function(msg){
        console.log(msg);
        var str = "";
        //方法一 for()循环
        for(var i = 0; i < msg.length; i++){
          str += 
            '<tr>\
             <td>'+ msg[i].id +'</td>\
             <td>'+ msg[i].name +'</td>\
             <td>'+ msg[i].age +'</td>\
             <td>'+ msg[i].gender +'</td>\
             </tr>'
        };

        //方法二 forEach()方法
        msg.forEach(function(item){
          console.log(item)
          str += 
            '<tr>\
             <td>'+ item.id +'</td>\
             <td>'+ item.name +'</td>\
             <td>'+ item.age +'</td>\
             <td>'+ item.gender +'</td>\
             </tr>'
        });
       $('tbody').html(str);
      });
    });
  </script>
</body>
</html>